/**
 * 
 */
package com.feng.dao;

import com.feng.model.WrongBill;
import com.feng.util.PageResult;
import com.feng.util.Search;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


/**
 * 帐单DAO
 */
@Repository
public class WrongBillDao extends BaseDao<WrongBill> {
	
	/**
	 * 分页查询代理商
	 * @return
	 */
	public PageResult<WrongBill> findByPage(Search search)  throws Exception {
		PageResult<WrongBill> ret = new PageResult<WrongBill>();
		ret.setCurrentPage(search.getCurrentPage());
		ret.setPageSize(search.getPageSize());
		
		String hql = "from WrongBill u where 1=1 ";
		List<Object> params = new ArrayList<Object>();
		if (!StringUtils.isEmpty(search.getNo()) ) {
			hql += " and u.insuranceNo like ? escape '/' ";
			params.add("%" + search.getNo() + "%");
		}
		if (!StringUtils.isEmpty(search.getCompany()) ) {
			hql += " and u.company like ? escape '/' ";
			params.add("%" + search.getCompany() + "%");
		}
		if (!StringUtils.isEmpty(search.getName()) ) {
			hql += " and u.name like ? escape '/' ";
			params.add("%" + search.getName() + "%");
		}
		if (!StringUtils.isEmpty(search.getLicensePlate()) ) {
			hql += " and u.licensePlate like ? escape '/' ";
			params.add("%" + search.getLicensePlate() + "%");
		}
		if (search.getOp()!=null) {
			hql += " and u.op =?";
			params.add(search.getOp());
		}
		//导入开始时间
		if (search.getFromDate() != null ) {
			hql += " and u.optime >= ?";
			params.add(search.getFromDate());
		}

		//导入结束时间
		if (search.getToDate() != null) {
			hql += " and u.optime <= ?";
			Calendar toDate = Calendar.getInstance();
			toDate.setTime(search.getToDate() );
			toDate.add(Calendar.DATE, 1);
			params.add(toDate.getTime());
		}

		if (!StringUtils.isEmpty(search.getOrderBy()) ) {
			hql += " order by "+search.getOrderBy();
		}

		this.findPageList(hql, params,ret);
	    return ret;
	}


	public WrongBill findByNo(String insuranceNo){
		List<WrongBill> list = this.find("from WrongBill u where u.insuranceNo = ?",insuranceNo);
		WrongBill ret = null;
		if(list.size()>0){
			ret = list.get(0);
		}
		return ret;
	}

	public WrongBill findByName(String name){
		List<WrongBill> list = this.find("from WrongBill u where u.name = ?",name);
		WrongBill ret = null;
		if(list.size()>0){
			ret = list.get(0);
		}
		return ret;
	}
	public WrongBill findByPlate(String licensePlate){
		List<WrongBill> list = this.find("from WrongBill u where u.licensePlate = ?",licensePlate);
		WrongBill ret = null;
		if(list.size()>0){
			ret = list.get(0);
		}
		return ret;
	}

}
